package hu.tortavar2.client.activity;

import java.util.List;

import hu.tortavar2.client.ClientFactory;
import hu.tortavar2.client.SiteService;
import hu.tortavar2.client.SiteServiceAsync;
import hu.tortavar2.client.place.AddNewProductPlace;
import hu.tortavar2.client.view.adminpages.AddNewProductPage;
import hu.tortavar2.client.view.adminpages.AddNewProductPageImpl;
import hu.tortavar2.shared.ProductCategory;
import hu.tortavar2.shared.Shop;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class AddNewProductActivity extends AbstractActivity implements AddNewProductPage.Presenter {
	
	SiteServiceAsync siteService = GWT.create(SiteService.class);

	private final AddNewProductPage view;
	private final PlaceController placeController;
	
	public AddNewProductActivity(AddNewProductPlace place, ClientFactory clientFactory) {
		view = new AddNewProductPageImpl();
		placeController = clientFactory.getPlaceController();
		refreshCategory();
		getBlobstoreUploadUrl();
	}
	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view.setPresenter(this);
		panel.setWidget(view.asWidget());
	}

	@Override
	public void goTo(Place place) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAddNewProductClicked() {
		// TODO Auto-generated method stub
		view.submitAddNewProductForm();
	}

	@Override
	public void refreshCategory() {
		siteService.getProductCategory(new AsyncCallback<List<ProductCategory>>() {
			
			@Override
			public void onSuccess(List<ProductCategory> result) {
				// TODO Auto-generated method stub
				view.setCategory(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public void getBlobstoreUploadUrl() {
		siteService.getBlobStoreUploadUrl(new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				view.setBlobstoreUploadUrl(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}
